package Inits

import (
	"diablo-gin/configs"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/jinzhu/gorm"
	"strconv"
	"sync"
)

var mysqlOnce sync.Once

var Conn *gorm.DB

//单例模式连接
func DbInit() *gorm.DB {
	mysqlOnce.Do(func() {
		args := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local",
			configs.DbUserName,
			configs.DbPassword,
			configs.DbHost,
			strconv.Itoa(configs.DbPort),
			configs.DbName,
		)
		db, err := gorm.Open(configs.DbDriver, args)
		if err != nil {
			panic(err.Error())
		}
		Conn = db
	})
	return Conn
}
